﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Atom;

namespace YouHP.Pages.TreatmentHistory
{
    public class ModifyPrescription : PatientBasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected PrescriptionInfo _presInfo;

        protected void HandleCommon()
        {
            this.AddLocation("修改方剂", "/TreatmentHistory/ModifyPrescription.aspx");
            this.SetTitle("修改方剂");
            int presId = RequestTools.GetInt("prescriptionid", 0);
            if (presId <= 0)
            {
                this.AddErrLine("错误的方剂id！");
                return;
            }

            _presInfo = PrescriptionBLL.Instance.GetPrescriptionInfo(presId);
            if (_presInfo.Id <= 0)
            {
                this.AddErrLine("错误的方剂id！");
                return;
            }

            int prId = UserData.Id;
            if (prId <= 0)
            {
                this.AddErrLine("你还没有登录!");
                return;
            }
        }

        protected void HandleGet()
        {
 
        }

        protected void HandlePost()
        {
            PrescriptionInfo presInfo = ValidateForm();
            if (presInfo == null)
            {
                this.AddMsgLine("输入有误");
                return;
            }

            bool result = PrescriptionBLL.Instance.UpdatePrescriptionInfo(presInfo);

            if (result)
            {
                if (!DoctorBLL.Instance.UpdateDoctorPrescriptionAverageScoreAndCount(presInfo.DoctorId))
                    result = false;
            }

            if (result)
            {
                this.AddMsgLine("修改成功");
            }
            else
            {
                this.AddErrLine("修改失败");
            }

            this.SetUrl("/TreatmentHistory/Index.aspx");
        }

        protected PrescriptionInfo ValidateForm()
        {
            if (_presInfo.Id <= 0)
                return null;

            PrescriptionInfo presInfo = PrescriptionBLL.Instance.GetPrescriptionInfo(_presInfo.Id);
            if (presInfo.Id <= 0)
                return null;

            bool makeChange = false;

            string presName = RequestTools.GetString("PrescriptionName").Trim();
            if (presName == "")
                return null;

            if (string.Compare(presName, _presInfo.Name) != 0)
            {
                presInfo.Name = presName;
                makeChange = true;
            }

            string presDesc = RequestTools.GetString("PrescriptionDescription").Trim();
            if (presDesc == "")
                return null;

            if (string.Compare(presDesc, _presInfo.Description) != 0)
            {
                presInfo.Description = presDesc;
                makeChange = true;
            }

            string presComment = RequestTools.GetString("PrescriptionComment").Trim();
            if (presComment == "")
                return null;

            if (string.Compare(presComment, _presInfo.Comment) != 0)
            {
                presInfo.Comment = presComment;
                makeChange = true;
            }

            double presScore = RequestTools.GetDouble("PrescriptionScore", _presInfo.Score);
            if (presScore != _presInfo.Score)
            {
                presInfo.Score = presScore;
                makeChange = true;
            }

            if (!makeChange)
                return null;

            return presInfo;
        }
    }
}